package com.string;
// 字符串缓冲
public class HelloStringBuffer {
	public static int DEFALUT_CAPACITY = 16;
	private int capacity;
	// 模拟实现一个 StringBuffer
	private char[] data = null; // 存储
	private int writeIndex = 0; // 记录写下标位置
	// 无参构造
	public HelloStringBuffer() {
		initData();
	}
	// 默认空间容量构造
	public HelloStringBuffer(int capacity) {
		initData(capacity);
	}
	// 默认字符串构造
	public HelloStringBuffer(String baseString) {
		initData(baseString);
	}
	// 默认数据构造
	public HelloStringBuffer(char[] baseData) {
		initData(baseData);
	}
	
	// 追加一个数据值
	public HelloStringBuffer append(Object value){
		String value_str = String.valueOf(value);
		
		return append(value_str.toCharArray());
	}
	
	// 追加一个char数组值
	public HelloStringBuffer append(char[] value){
		if(value.length>capacity - writeIndex){ // 扩容
			augmentCapacity(value.length);
		}
		writeData(value, 0, writeIndex);
		return this;
	}
	// 拷贝一个到新的 HelloStringBuffer
	public HelloStringBuffer copyString(int start,int end){
		int length = end - start;
		if(start<0){
			throw new ArrayIndexOutOfBoundsException("start 必须大于等于0,传入值："+start);
		}
		if(end > writeIndex){
			throw new ArrayIndexOutOfBoundsException("传入 end:"+end+" 超出,max:"+writeIndex);
		}
		if(length<0){
			throw new ArrayIndexOutOfBoundsException("start 不能小于 end");
		}
		char[] copy = new char[length];
		System.arraycopy(data, start, copy, 0, length);
		return new HelloStringBuffer(copy);
	}
	// 截取字符串
	public String subString(int start,int end){
		int length = end - start;
		if(start<0){
			throw new ArrayIndexOutOfBoundsException("start 必须大于等于0,传入值："+start);
		}
		if(end > writeIndex){
			throw new ArrayIndexOutOfBoundsException("传入 end:"+end+" 超出,max:"+writeIndex);
		}
		if(length<0){
			throw new ArrayIndexOutOfBoundsException("start 不能小于 end");
		}
		char[] copy = new char[length];
		System.arraycopy(data, start, copy, 0, length);
		return String.valueOf(copy);
	}
	
	public int length(){
		return writeIndex;
	}
	@Override
	public String toString() {

		return String.valueOf(data, 0, writeIndex);
	}
	// 初始化缓存区
	private final void initData(){
		if(capacity <= 0){		// 空间为0 设置默认空间
			capacity = DEFALUT_CAPACITY;
		}
		data = new char[capacity];
	}
	private final void initData(int capacity){
		this.capacity = capacity;	// 设置空间
		initData();					// 初始化缓存
	}
	private final void initData(String baseString){
		initData(baseString.toCharArray());
	}
	
	private final void initData(char[] baseData){
		int capacity = baseData.length*2;
		initData(capacity);	//初始化字符长度两倍的缓存
		writeData(baseData, 0, 0); //将字符写进去
	}
	
	// 写入数据 srcStart 传入源写入开始位置,start 写入data的位置
	private int writeData(char[] srcData,int srcStart,int start){
		int dataLength = data.length;
		int srcLength = srcData.length;
		int totalLength = start+srcLength;
		int residueLength = 0;
		int length = srcLength;
		if(totalLength > dataLength){
			length = dataLength-start;
			residueLength = totalLength - dataLength;
		}
		System.arraycopy(srcData, srcStart, data, start, length);
		writeIndex = start+length;
		return residueLength;
	}
	// 扩充容量
	private void augmentCapacity(int length){
		char[] old_data = data;
		data = new char[capacity = (capacity+length)*2]; //增加容量为原来的2倍
		System.arraycopy(old_data, 0, data, 0, old_data.length);
	}
	// 测试
	public static void main(String[] args) {
		HelloStringBuffer x = new HelloStringBuffer("11");
		
		System.out.println(x.append("12345678901234567890_"));
		System.out.println(x.append("12345678901234567890_"));
		System.out.println(x.append("12345678901234567890_"));
		System.out.println(x.append("12345678901234567890_"));
		
		System.out.println(x.copyString(0,1));
		System.out.println(x.subString(0,1));
		
		
	}
}
